﻿
package com.ostrich.manpower.service.impl;

import java.util.List;

import org.apache.log4j.Logger;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.base.service.BaseService;
import com.jeecg.entity.SysUser;
import com.ostrich.manpower.bean.TostHrBaseinfo;
import com.ostrich.manpower.bean.TostHrSalaryinfo;
import com.ostrich.manpower.bean.TostHrZhongheinfo;
import com.ostrich.manpower.common.ImportUtil;
import com.ostrich.manpower.service.TostHrZhongheinfoDao;
/**
 * @author wangyinan
 * @brief    模块功能简单描述
 * @details  模块功能详细描述
 */
@Service("tostHrZhongheinfoService")
public class TostHrZhongheinfoService<T> extends BaseService<T> {
	private final static Logger log= Logger.getLogger(TostHrZhongheinfoService.class);

	@Autowired(required=false) //自动注入，不需要生成set方法了，required=false表示没有实现类，也不会报错。
	private TostHrBaseinfoService<TostHrBaseinfo> tostHrBaseinfoService; 
	
	@Autowired
    private TostHrZhongheinfoDao<T> dao;
	
	private  String setstr = null;
	
	public TostHrZhongheinfoDao<T> getDao() {
		return dao;
	}
	

	
	public String setZhongheiBySheet(HSSFSheet sheet) throws Exception{
		//List<TostHrMealaddinfo> infos =new ArrayList<TostHrMealaddinfo>();
	    HSSFRow row = null;
	    int colnum = 8;
		int s = sheet.getPhysicalNumberOfRows();
		String msg = "";
		 //遍历该行所有的行,j表示行数 getPhysicalNumberOfRows行的总数  除去标题跟表头
	     for(int j = 1; j < s; j++) {
	         row = sheet.getRow(j);
	         TostHrZhongheinfo tzhonghe= new TostHrZhongheinfo();
	         String strHrCode = ImportUtil.getCellValue(row.getCell(1));
	         String strName = ImportUtil.getCellValue(row.getCell(2));
	         //查询第二列中的员工编号是否存在，如果存在才进行综合计算信息的添加，否则不增加
	         TostHrBaseinfo bainfo= (TostHrBaseinfo) tostHrBaseinfoService.getBaseinfoByHrCode(strHrCode);
	         System.out.println(strHrCode);
	         if(strHrCode!=null){
					if(bainfo==null||bainfo.getId() == null){
						msg +="第"+(j+1)+"行记录异常：工号："+strHrCode+"的数据没有正常导入（工号不存在）。";
						continue;
					}
				}
	         if(!(strName.trim().equals(bainfo.getHrName()))){
	        	 msg +="第"+(j+1)+"行记录异常：工号："+strHrCode+"，姓名为："+ImportUtil.getCellValue(row.getCell(2))+"的没有正常导入（工号跟姓名不匹配）。";
	        	 continue;
	         }
	         
	         for(int i=0;i<colnum;i++ ){
	        	 String str = ImportUtil.getCellValue(row.getCell(i));
	        	 if(i==1){//hr_code
	        		 TostHrBaseinfo bainfo1 = (TostHrBaseinfo)tostHrBaseinfoService.getBaseinfoByHrCode(str);
	        		 tzhonghe.setHrId(bainfo1.getId());
	        	 }else if(i==3){//绩效的系数
	        		 Float flstr = (str!=null  && str.trim().length()>0 && str.matches("^[0-9]+\\.{0,1}[0-9]{0,2}$"))?Float.parseFloat(str):new Float(0);
	        	     tzhonghe.setHzhCoefficient(flstr);
	        	 }else if(i==4){// 提成金额
	        		 Float flstr = (str!=null  && str.trim().length()>0 && str.matches("^[0-9]+\\.{0,1}[0-9]{0,2}$"))?Float.parseFloat(str):new Float(0);
	        		 tzhonghe.setHzhPushmoney(flstr);
	        	 }else if(i==5){//其他扣款金额
	        		 Float flstr = (str!=null  && str.trim().length()>0 && str.matches("^[0-9]+\\.{0,1}[0-9]{0,2}$"))?Float.parseFloat(str):new Float(0);
	        		 tzhonghe.setHzhOtherdeduct(flstr);
	        	 }else if(i==6){//补发金额
	        		 Float flstr = (str!=null  && str.trim().length()>0 && str.matches("^[0-9]+\\.{0,1}[0-9]{0,2}$"))?Float.parseFloat(str):new Float(0);
	        		 tzhonghe.setHzhBackpay(flstr);
	        	 }else if(i==7){//月份
	        		 if(str!=null  && str.trim().length()>0){
	        			 String[] str1 = str.split("\\.");
	        			 setstr = str1[0];
	        		 }
	        		 tzhonghe.setHzhMont(setstr);
	        	 }
	         	}
	         if(tzhonghe!=null){
	        	//根据员工编号查询是否有该员工
	        	 TostHrZhongheinfo  zhonghe= queryZhongheByhrid(tzhonghe.getHrId(),tzhonghe.getHzhMont());
	        	 if(zhonghe!=null ){
	        		 tzhonghe.setId(zhonghe.getId());
	        		 dao.update((T) tzhonghe);
	        	 }else{
	        		 dao.add((T) tzhonghe);
	        	 }
	         }
	     }
	     return msg;
	}
	
	public List<T> queryByMap(TostHrZhongheinfo page) throws Exception{
		Integer rowCount = queryByMapCount(page);
		page.getPager().setRowCount(rowCount);
		return getDao().queryByMap(page);
	}
	public int queryByMapCount(TostHrZhongheinfo page)throws Exception{
		return getDao().queryByMapCount(page);
	}
	public int queryByMapCount(TostHrZhongheinfo page,String startM,String endM)throws Exception{
		return getDao().queryByMapCount(page);
	
	}
	public TostHrZhongheinfo queryZhongheByhrid(Integer hrId,String hzhMont){
		return getDao().getZhongheByhrid(hrId,hzhMont);
	}
}
